<?php 
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class AdController extends AdminBaseController{
	public function index(){
        $ad = M('ad');
        $list = $ad->field('id,sort,title,imgs,type,position,status,hits,addtime,downtime')->order('id desc')->select();
        $this->assign('list',$list);
        $this->display();
    }
	public function add(){
        $pid = I('get.pid/d',0);
        $adInfo = M('ad')->field('id,sort,title,imgs,type,position,status,hits')->select();
        $posInfo = M('position')->field('id,title,type')->select();
        $this->assign('posInfo',$posInfo);
        $this->assign('adInfo',$adInfo);
        $this->display();
    }
    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(!$id){
                $this->error('非法操作！');
            }
            $adInfo = M('ad')->field('id,sort,title,imgs,type,position,status,downtime,hits')->where(['id'=>$id])->find();
            $posInfo = M('position')->field('id,title,type')->select();
            $this->assign('posInfo',$posInfo);
            $this->assign('adInfo',$adInfo);
            $this->display('ad/add');
        }
    }
    public function save(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            $sort = I('post.sort/d',0);
            $title = I('post.title/s','');
            if(!$title){
                $this->ajaxReturn(['status'=>1,'msg'=>'广告名称不能为空！']);
            }
            $type = I('post.type/s',0);
            if(!$type){
                $this->ajaxReturn(['status'=>1,'msg'=>'广告类型不能为空！']);
            }
            $position = I('post.position/s','');
            if(!$position){
                $this->ajaxReturn(['status'=>1,'msg'=>'所属版位不能为空！']);
            }
            $img_path = '';
            if(!empty($_FILES)){
                $img_path = uploadFile($_FILES['imgs']);
            }
            if(is_array($img_path)){
                $this->ajaxReturn($img_path);
            }
            $status = I('post.status/d',1);
            $downtime = I('post.downtime/s','');
            $ad = M('ad');
            $posNum = $ad->where(['title'=>$title,'type'=>$type,'position'=>$position])->count();
            if($posNum > 0){
                $this->ajaxReturn(['status'=>1,'msg'=>'版位已存在！']);
            }
            $data = [
                'id' => $id,
                'sort' => $sort,
                'title'=>$title,
                'imgs'=>$img_path,
                'type'=>$type,
                'position'=>$position,
                'status'=>$status,
                'hits' => 'hits + 1',
                'downtime'=>$downtime
            ];
            $ad = M('ad');
            $isSQL = $ad->add($data,[],true);
            if(!$isSQL){
                $this->ajaxReturn(['status'=>1,'msg'=>'失败！']);
            }
            $this->ajaxReturn(['status'=>0,'msg'=>'成功！']);
        }
    }
    public function changeState(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
            }
            $adInfo = M('ad')->field('status')->where(['id'=>$id])->find();
            if(empty($adInfo['status'])){
                M('ad')->where(['id' => $id])->setField('status',1);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已开启' , 'data' => 1]);
            }else{
                M('ad')->where(['id' => $id])->setField('status',0);
                $this->ajaxReturn(['status' => 0 , 'msg' => '已禁用' , 'data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作！']);
        }
    }
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
            }
            $isDel = M('ad')->where(['id' => $id])->delete();
            if(!$isDel){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
        }
    }
}